from selenium import webdriver

from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options


def initialize_webdriver(is_no_image=False):
    chrome_options = Options()

    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
    chrome_options.add_argument('--disable-blink-features=AutomationControlled')
    chrome_options.add_experimental_option('useAutomationExtension', False)
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("accept-language=zh-CN,zh;q=0.8,zh-TW;q=0.7,en-US;q=0.3,en;q=0.2")

    if is_no_image:
        chrome_options.add_argument("--blink-settings=imagesEnabled=false")
        chrome_options.add_argument('--disable-javascript')

    chrome_options.binary_location = r"C:\Program Files\Google\Chrome\Application\Chrome\App\chrome.exe"

    # 指定 ChromeDriver 路径
    chrome_driver_path = r"D:\chromedriver_win32\chromedriver.exe"
    service = Service(executable_path=chrome_driver_path)

    return webdriver.Chrome(service=service, options=chrome_options)
